<template>
  <rt-view>
    <rt-header>
      <rt-navbar>
        <rt-search
          v-focus
          v-model="name"
          placeholder="搜索"
          @on-search="onSearch()"
        ></rt-search>
      </rt-navbar>
    </rt-header>
    <rt-content ref="content">
      <rt-list ref="articalList" :fetch="fetch" @toggle="toggle">
        <div slot-scope="{ row }" @click="clickToDetail(row)">
          <rt-swipe-cell :right-width="100" @click="btuClick">
            <div class="list_content">
              <div class="desc">
                <div class="tille">{{ row.object_name }}</div>
                <div class="footer">
                  {{ row.readnum }} 阅读 . {{ row.commentnum }} 评论
                </div>
              </div>
              <div class="imgs">
                <img :src="showRemotePic(row.avatar)" @error="showErrorPic($event)"/>
              </div>
            </div>
            <div slot="right">
              <div class="img-show" @click="clickMe(row)">
                <img src="../../assets/imgs/postlike.png" />
              </div>
              <!--<rt-swipe-cell-button type="danger" text="取消点赞" />-->
            </div>
          </rt-swipe-cell>
        </div>
      </rt-list>
    </rt-content>
  </rt-view>
</template>

<script>
export default {
  name: 'MylikeLog',
  filters: {
    nameStyle(value) {
      if (!value) return '';
      if (value.length > 16) {
        return value.slice(0, 16) + '...';
      }
      return value;
    }
  },
  data() {
    return {
      type: '2',
      pageIndex: 0,
      userId: '',
      name: ''
    };
  },
  methods: {
    fetch(page) {
      return this.getDataList(page).then(resp => {
      /*  if (this.pageIndex > 1) {
          this.pageIndex = 0;
          return [];
        }*/
        this.pageIndex++;
        return resp;
      });
    },
    getDataList(page) {
      return new Promise(resolve => {
        const data = {
          type: this.type,
          pageIndex: page.pageIndex,
          pageSize: page.pageSize,
          name: this.name
        };
        rt.get(`/api/bbs/postlikelog/mylikelist`, data)
          .then(res => {
            resolve(res);
          })
      });
    },
    clickToDetail(row) {
      var param = {
        object_id: row.object_id,
        object_name: row.object_name
      };
      rt.get(`/api/bbs/article/detail/${row.object_id}`)
        .then(res => {
          if (res.bbs_article_status === '2') {
            rt.post(`/api/bbs/openedlog/saveopenlog/article`, param)
              .then(res => {
                this.$router.push({
                  name: 'article-detail',
                  query: {
                    id: row.object_id,
                    name: row.object_name
                  }
                });
              })
              .catch(error => {
                console.log(error);
              });
          } else {
            rt.alert('哎呀，文章丢掉啦');
          }
        })
        .catch(error => {
          console.log(error);
        });
    },
    onSearch() {
      this.$refs['articalList'].refreshData(1);
    },
    toggle(showHeader) {
      this.showHeader = showHeader;
      this.$refs.content.resizeStyle();
    },
    btuClick(position) {
      console.log(position);
    },
    clickMe(val) {
      const data = {
        object_id: val.object_id,
        object_name: val.object_name
      };
      rt.post(`/api/bbs/postlikelog/postlike`, data)
        .then(res => {
          this.$refs['articalList'].refreshData(1);
        })
        .catch(error => {
          console.log(error);
        });
    },
    showRemotePic(path){
      return rt.showRemotePic(path);
    },
    showErrorPic(e){
      rt.showErrorPic(e);
    }
  }
};
</script>
